package com.soap.desensitize.util;

import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
 * @desc 不对称加密公私钥工具类
 */
public class AsymmetricUtil {
    private PrivateKey privateKey;
    private PublicKey publicKey;
    private String algorithm;
    public AsymmetricUtil(String algorithm) throws NoSuchAlgorithmException {
        this.algorithm=algorithm;
        //生成密钥对对象
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);

        //生成密钥对
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        //生成公钥
        this.publicKey = keyPair.getPublic();
        //生成私钥
        this.privateKey = keyPair.getPrivate();
    }
    public AsymmetricUtil(String algorithm,int size) throws NoSuchAlgorithmException {
        this.algorithm=algorithm;
        //生成密钥对对象
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
        keyPairGenerator.initialize(size);
        //生成密钥对
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        //生成公钥
        this.publicKey = keyPair.getPublic();
        //生成私钥
        this.privateKey = keyPair.getPrivate();
    }
    /**
     * 公钥字符串还原为公钥
     *
     * @param publicKeyString 公钥字符串
     * @return 公钥
     * @throws Exception
     */
    public PublicKey publicKeyStringToKey(String publicKeyString) throws Exception {
        byte[] publicBytes = Base64.getDecoder().decode(publicKeyString);
        KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
        PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicBytes));
        return publicKey;
    }

    /**
     * 私钥字符串还原为私钥
     *
     * @param privateKeyString 私钥字符串
     * @return 私钥
     * @throws Exception
     */
    public PrivateKey privateKeyStringToKey(String privateKeyString) throws Exception {
        byte[] privateBytes = Base64.getDecoder().decode(privateKeyString);
        KeyFactory keyFactory = KeyFactory.getInstance(algorithm);

        PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateBytes));
        return privateKey;
    }

    /**
     * 返回公钥字节数组
     *
     * @return
     */
    public byte[] publicKeyEncoded() {
        return this.publicKey.getEncoded();
    }

    /**
     * 返回私钥字节数组
     *
     * @return
     */
    public byte[] privateKeyEncoded() {
        return this.privateKey.getEncoded();
    }

    /**
     * 公钥byteToString转码
     *
     * @return
     */
    public String publicKeyToString() {
        return Base64.getEncoder().encodeToString(publicKeyEncoded());
    }

    /**
     * 私钥byteToString转码
     *
     * @return
     */
    public String privateKeyToString() {
        return Base64.getEncoder().encodeToString(privateKeyEncoded());
    }

    public PrivateKey getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(PrivateKey privateKey) {
        this.privateKey = privateKey;
    }

    public PublicKey getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(PublicKey publicKey) {
        this.publicKey = publicKey;
    }
}
